package com.itboye.textileexpres.myset;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

import com.itboye.textileexpes.adapter.KefuAdapter;
import com.itboye.textileexpes.beans.NewsKefuBeans;
import com.itboye.textileexpes.presenter.AppUpdatePresenter;
import com.itboye.textileexpres.R;
import com.itboye.textileexpres.base.BaseFragment;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.WebActivity;

public class FragmentCustomer extends BaseFragment implements Observer {
	ExpandableListView expandlsitview;

	// AdapterCustomerService adapterCustomerService;
	KefuAdapter adapter;
	AppUpdatePresenter appUpdatePresenter;

	List<NewsKefuBeans> list = new ArrayList<NewsKefuBeans>();

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.fragment_customer;
	}

	@Override
	public void onMyActivityCreated() {
		// TODO Auto-generated method stub
		appUpdatePresenter = new AppUpdatePresenter(this);
		appUpdatePresenter.onKefu();
		onExpandGroup();
		expandlsitview.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), WebActivity.class);
				intent.putExtra(
						"url","http://api.guannan.itboye.com/public/web.php/Alibaichuan/im?uid=115&touid=114");
				startActivity(intent);
				return true;
			}
		});
		expandlsitview.setOnGroupClickListener(new OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				// TODO Auto-generated method stub
				
				return true;
			}
		});

	}

	public void onExpandGroup() {

		expandlsitview.setGroupIndicator(null);
		// 默认全部展开
		for (int i = 0; i < list.size(); i++) {
			expandlsitview.expandGroup(i);
			System.out.println(">>>>>>>>>>>>>>>执行了");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == appUpdatePresenter.KEfu_success) {

				list = (List<NewsKefuBeans>) entity.getData();
				adapter = new KefuAdapter(list, getActivity());
				expandlsitview.setAdapter(adapter);
				onExpandGroup();

			}
		}
	}

}
